package com.hqyj.springBoot.modules.common.controller;

import com.hqyj.springBoot.modules.common.entity.ImageType;
import com.hqyj.springBoot.modules.common.entity.ResultEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yun
 * @date 2021-5-5 11:48
 */
@RestController
@RequestMapping("/api")
public class DictionaryController {

    private final static Logger LOGGER = LoggerFactory.getLogger(DictionaryController.class);

    //通过反射获取class文件进而获取类中的方法
    private static Map<String,Class> dictionaryMap = new HashMap<String, Class>(){{
        put("imageType", ImageType.class);
        put("resultStatus", ResultEntity.ResultStatus.class);
    }};
//    static{
//        dictionaryMap.put("imagetype", ImageType.class);
//        dictionaryMap.put("resultStatus", ResultEntity.ResultStatus.class);
//    }

    /**
     * 127.0.0.1/api/dictionary/imageType ---- get
     */
    @GetMapping("/dictionary/{enumName}")
    public List<Object> dictionary(@PathVariable String enumName){
        //如果集合中有这个名字在通过反射拿到Class文件类
            try {
                if(dictionaryMap.containsKey(enumName)){
                    Class clazz = dictionaryMap.get(enumName);
                    Method method = clazz.getDeclaredMethod("values");//通过这个函数获得指定的方法
                    Object[] result = (Object[]) method.invoke(null);//通过这个函数来执行class中的方法
                    return Arrays.asList(result);
                }
            } catch (Exception e) {
                e.printStackTrace();
                LOGGER.debug(e.getMessage());
            }

        return null;
    }
}
